﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class DayToDayManagement_ProductDownStatistics : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string DownTypeCondition
    {
        get
        {
            string condition = "";
            if (ViewState["DownTypeCondition"] != null)
            {
                condition = ViewState["DownTypeCondition"].ToString();
            }
            return condition;
        }
        set
        {
            ViewState["DownTypeCondition"] = value;
        }
    }

    private string Condition
    {
        get
        {
            string condition = "";
            if (ViewState["Condition"] != null)
            {
                condition = ViewState["Condition"].ToString();
            }
            return condition;
        }
        set
        {
            ViewState["Condition"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300100105");
        if (!IsPostBack)
        {
            AspNetPager1.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString());
            BindDropDownOperator();
            BindDropAuditor();
            //BindRepeater("");
        }
    }

    #region 绑定控件
    private void BindDropDownOperator()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        dropDownOperator.DataSource = users;
        dropDownOperator.DataTextField = "USERS_NAME";
        dropDownOperator.DataValueField = "USERS_CODE";
        dropDownOperator.DataBind();
        dropDownOperator.Items.Insert(0, "");
        

    }

    private void BindDropAuditor()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        dropAuditor.DataSource = users;
        dropAuditor.DataTextField = "USERS_NAME";
        dropAuditor.DataValueField = "USERS_CODE";
        dropAuditor.DataBind();
        dropAuditor.Items.Insert(0, "");
    }


    private void BindRepeater(string sqlCondition)
    {
        int rowCount;
        DataSet dst = new BLL.CMDDOWN().GettProductDownStatistics(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, sqlCondition, out rowCount);
        rptDownStatistics.DataSource = dst;
        rptDownStatistics.DataBind();
        AspNetPager1.RecordCount = rowCount;
    }

    #endregion


    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindRepeater(this.Condition);
    }

    /// <summary>
    /// 分页时计算记录起始下标
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bool IsFirstCondition = true; //标识是否是第一个where条件
        StringBuilder sqlCondition = new StringBuilder();
        sqlCondition.Append("where ");
        if (hiddenDownTypes.Value != "")
        {
            sqlCondition.Append("DT_CODE in (");
            sqlCondition.Append(hiddenDownTypes.Value);
            sqlCondition.Append(") ");
            IsFirstCondition = false;
        }
        if (dropDownOperator.SelectedIndex != 0)
        {
            if (IsFirstCondition)
            {
                sqlCondition.Append("CD_DOWNER ='");
                sqlCondition.Append(dropDownOperator.SelectedItem.Text);
                sqlCondition.Append("' ");
                IsFirstCondition = false;
            }
            else
            {
                sqlCondition.Append("and CD_DOWNER ='");
                sqlCondition.Append(dropDownOperator.SelectedItem.Text);
                sqlCondition.Append("' ");
            }
        }
        if (dropAudit.SelectedIndex != 0)
        {
            if (IsFirstCondition)
            {
                sqlCondition.Append("CD_CHECK =");
                sqlCondition.Append(dropAudit.SelectedItem.Value);
                sqlCondition.Append(" ");
                IsFirstCondition = false;
            }
            else
            {
                sqlCondition.Append("and CD_CHECK =");
                sqlCondition.Append(dropAudit.SelectedItem.Value);
                sqlCondition.Append(" ");
            }
        }
        if (dropAuditor.SelectedIndex != 0)
        {
            if (IsFirstCondition)
            {
                sqlCondition.Append("CD_CHECKER='");
                sqlCondition.Append(dropAuditor.SelectedItem.Value);
                sqlCondition.Append("' ");
                IsFirstCondition = false;
            }
            else
            {
                sqlCondition.Append("and CD_CHECKER='");
                sqlCondition.Append(dropAuditor.SelectedItem.Value);
                sqlCondition.Append("' ");
            }
        }
        if (hiddenProducts.Value != "")
        {
            if (IsFirstCondition)
            {
                sqlCondition.Append("shangpin.spdm in (");
                sqlCondition.Append(hiddenProducts.Value);
                sqlCondition.Append(") ");
                IsFirstCondition = false;
            }
            else
            {
                sqlCondition.Append("and shangpin.spdm in (");
                sqlCondition.Append(hiddenProducts.Value);
                sqlCondition.Append(") ");
            }
        }
        
        try
        {
            if (txtDownStartTime.Text != "" && txtDownEndTime.Text != "")
            {
                if (IsFirstCondition)
                {
                    sqlCondition.Append("CD_DOWNDATE>'");
                    sqlCondition.Append(DateTime.Parse(txtDownStartTime.Text));
                    sqlCondition.Append("' and CD_DOWNDATE<'");
                    sqlCondition.Append(DateTime.Parse(txtDownEndTime.Text));
                    sqlCondition.Append("' ");
                    IsFirstCondition = false;
                }
                else
                {
                    sqlCondition.Append("and CD_DOWNDATE>'");
                    sqlCondition.Append(DateTime.Parse(txtDownStartTime.Text));
                    sqlCondition.Append("' and CD_DOWNDATE<'");
                    sqlCondition.Append(DateTime.Parse(txtDownEndTime.Text));
                    sqlCondition.Append("' ");
                }
            }
            if (!IsFirstCondition)
            {
                this.Condition = sqlCondition.ToString();
            }
            else
            {
                this.Condition = "";
            }
            BindRepeater(this.Condition);
        }


        catch (FormatException EX)
        {
            Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('输入的日期格式有误，请重新输入。')</script>");
        }
        catch
        {
            Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('查询时出现错误，请重新查询。')</script>");
        }
    }
}
